package easy;

public class Solution_724 {

    public int pivotIndex(int[] nums) {
        int[] right = new int[nums.length+1];
        right[nums.length - 1] = nums[nums.length - 1];
        for (int i = nums.length - 2; i >= 0; i--) {
            right[i] = right[i + 1] + nums[i];
        }
        int sum = 0;
        for (int i = 0; i < nums.length; i++) {
            if (right[i + 1] == sum) {
                return i;
            }
            sum += nums[i];
        }
        return -1;
    }

    /*public int pivotIndex(int[] nums) {
        if (nums.length==1) {
            return 0;
        }
        int[] left = new int[nums.length];
        left[0] = nums[0];
        int[] right = new int[nums.length];
        right[nums.length-1] = nums[nums.length-1];
        for (int i = 1; i < nums.length; i++) {
            left[i] = left[i-1] + nums[i];
            right[nums.length - i - 1] = right[nums.length - i] + nums[nums.length - i - 1];
        }
        for (int i = 0; i < left.length; i++) {
            if (i==0) {
                if (right[1]==0) {
                    return i;
                }
            } else if (i==left.length-1) {
                if (left[left.length-2]==0) {
                    return i;
                }
            } else {
                if (left[i-1] == right[i+1]) {
                    return i;
                }
            }
        }
        return -1;
    }*/

    public static void main(String[] args) {
        Solution_724 model = new Solution_724();
        System.out.println(model.pivotIndex(new int[]{2}));
        System.out.println(model.pivotIndex(new int[]{0, 1}));
        System.out.println(model.pivotIndex(new int[]{1, 0}));
        System.out.println(model.pivotIndex(new int[]{1, 2}));
        System.out.println(model.pivotIndex(new int[]{1, 7, 3, 6, 5, 6}));
        System.out.println(model.pivotIndex(new int[]{1, 2, 3}));
        System.out.println(model.pivotIndex(new int[]{2, 1, -1}));
    }
}
